package org.breathe.datastruct.impl;

import org.breathe.datastruct.RedisBytes;
import org.breathe.datastruct.RedisData;
import org.breathe.internal.Dict;
import org.breathe.protocal.Resp;
import org.breathe.protocal.decode.BulkString;
import org.breathe.protocal.decode.RespArray;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author: breathe
 * @createTime: 2025-05-15
 */
public class RedisHash implements RedisData {
    private volatile long timeout = -1;
    private Dict<RedisBytes, RedisBytes> hash;
    public RedisHash() {
        this.hash = new Dict<>();
    }
    @Override
    public long timeout() {
        return timeout;
    }

    @Override
    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    @Override
    public List<Resp> convertToResp() {
        List<Resp> result = new ArrayList<>();
        if (hash.size() == 0) {
            return result;
        }
        for (Map.Entry<RedisBytes, Object> entry : hash.entrySet()) {
            Resp[] resp = new Resp[2];
            RedisBytes key = entry.getKey();
            RedisBytes value = (RedisBytes) entry.getValue();
            resp[0] = new BulkString(key);
            resp[1] = new BulkString(value);
            result.add(new RespArray(resp));
        }
        return result;
    }

    public int put(RedisBytes field, RedisBytes value) {
        return hash.put(field, value) == null ? 1 : 0;
    }
    public Dict<RedisBytes, RedisBytes> getHash() {
        return hash;
    }
    public int delete(List<RedisBytes> fields) {
        return  (int)fields.stream()
                .filter(field -> hash.remove(field) != null)
                .count();
    }
}
